package cn.itsource.user.controller;

import cn.itsource.basic.annotation.PreAuthorize;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageResult;
import cn.itsource.user.domain.User;
import cn.itsource.user.query.UserQuery;
import cn.itsource.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
     * 接口：添加或修改
     *
     * @param user 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PreAuthorize(name = "保存角色", sn = "user:save")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user) {
        if (user.getId() != null) {
            userService.update(user);
        } else {
            userService.add(user);
        }
        return new AjaxResult();

    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @PreAuthorize(name = "删除角色", sn = "user:delete")
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        userService.delete(id);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        userService.patchdel(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id") Long id) {
        return userService.findOne(id);
    }

    @GetMapping("/handleUser/{logininfoId}")
    public User handleUser(@PathVariable("logininfoId") Long logininfoId) {
        return userService.handleUser(logininfoId);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageResult<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryPage(query);
    }

    //修改状态
    @PutMapping("/handleState")
    public AjaxResult handleState(@RequestBody User user) {
        return userService.handleState(user);
    }

    //电话号注册
    @PostMapping("/register/phone")
    public AjaxResult registerPhone(@RequestBody Map<String, String> map) {
        try {
            userService.registerPhone(map);
            return new AjaxResult();
        } catch (BusinessException b) {
            b.printStackTrace();
            return new AjaxResult(false, b.getMessage()); //自己throw的异常
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    //邮箱注册
    @PostMapping("/register/email")
    public AjaxResult registerEmail(@RequestBody Map<String, String> map) {
        try {
            userService.registerEmail(map);
            return new AjaxResult();
        } catch (BusinessException b) {
            b.printStackTrace();
            return new AjaxResult(false, b.getMessage()); //自己throw的异常
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }

    //在qq邮箱激活员工状态 - 1启用，0禁用
    @GetMapping("/activation/{id}")
    public void activation(@PathVariable("id") Long id, HttpServletResponse response) {
        userService.activation(id, response);
    }

    //修改个人信息数据
    @PutMapping("/modifyUser")
    public AjaxResult modifyUser(@RequestBody User user) {
        try {
            return userService.modifyUser(user);
        } catch (BusinessException b) {
            b.printStackTrace();
            return new AjaxResult(false, b.getMessage()); //自己throw的异常
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
        }
    }
}
